#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

import sys
import ConfigParser
import time
import os


def checkSpace(dev):
    import commands
    import re

    space = 10
    line = commands.getoutput("df -B G -P")
    splitline = line.split("\n")
    for item in splitline:
        if item.startswith(dev):
            item = item.rstrip()
            regex = "^.*? *\d*G *\d*G *(\d*)G *\d*% *.*$"
            match = re.match(regex, item)
            if match:
                result = match.group(1)
                space = int(result)
                break
    return space


def countProcess():
    count = 0
    for line in os.popen("ps xa"):
        line = line.rstrip()
        if line.find("sh pmc_ftp.sh") != -1:
            count += 1
    return count


if __name__ == "__main__":
    print("Loading config file ..."),
    config = ConfigParser.ConfigParser()
    config.read(sys.argv[1])
    dev = config.get("node", "dev")
    time.sleep(1)
    print("complete.")
    print("Loading task list ..."),
    filelist = os.listdir(".")
    time.sleep(1)
    print("complete.")
    for filename in filelist:
        if filename.endswith(".task"):
            while checkSpace(dev) < 10 or countProcess() != 0:
                time.sleep(30)
            # taskid = filename.split(".")[-2]
            print("Start task " + filename + ".")
            time.sleep(1)

            print("Downloading task " + filename + "."),
            os.system("wget -q -x -i " + filename)
            time.sleep(1)
            print("complete.")

            print("Comporessing task " + filename + "."),
            os.system("tar -czf " + filename + ".tar.gz ftp.ncbi.nlm.nih.gov --remove-files")
            time.sleep(1)
            print("complete.")

            print("Calculating md5 of task " + filename + "."),
            os.system("md5sum " + filename + ".tar.gz >" + filename + ".tar.gz.md5")
            time.sleep(1)
            print("complete.")

            print("Renaming file of task " + filename + "."),
            os.rename(filename + ".tar.gz", filename + ".tar.gz.complete")
            time.sleep(1)
            print("complete.")

            print("Deleting task list " + filename + "."),
            os.remove(filename)
            time.sleep(1)
            print("complete.")

            time.sleep(1)
            print("End task " + filename + ".")
    os.system("touch all.done")
    print("All tasks complete.")
